package com.suncaper.tddmall.controller;

import com.suncaper.tddmall.common.model.User;
import com.suncaper.tddmall.common.model.User_address;
import com.suncaper.tddmall.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/my_account")
public class UserAccountAddressController {
    @Autowired
    AddressService addressService;
    @RequestMapping("/addresses")
    public String displayUserAddress(HttpSession httpSession, Model model){
        User user = (User) httpSession.getAttribute("user");
        List<User_address> addresses = addressService.searchByUserId(user.getUserId());
        model.addAttribute("addresses",addresses);
        return "../static/page/addresses";
    }

    @GetMapping("/address_details")
    public String searchByAddressId(@RequestParam("id") String addressId,Model model){
        User_address address = addressService.searchByAddressId(addressId);
        model.addAttribute("userAddress",address);
        return "../static/page/address_details";
    }

    @PostMapping("/address_update")
    public String updateByAddressId(User_address user_address){
        addressService.updateByAddressId(user_address);
        return  "redirect:/my_account/addresses";
    }

}
